package com.ruoyi.stuinfo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.grade.domain.Grade;
import com.ruoyi.grade.domain.Gradetemplate;
import com.ruoyi.stuinfo.domain.StudentActivity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stuinfo.domain.AcademicInfo;
import com.ruoyi.stuinfo.service.IAcademicInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学业情况Controller
 *
 * @author zc
 * @date 2025-05-18
 */
@RestController
@RequestMapping("/stuinfo/acadInfo")
public class AcademicInfoController extends BaseController
{
    @Autowired
    private IAcademicInfoService academicInfoService;

    /**
     * 查询学业情况列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:acadInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcademicInfo academicInfo)
    {

        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<AcademicInfo> list = academicInfoService.selectAcademicInfoList(academicInfo);
            return getDataTable(list);
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            startPage();
            List<AcademicInfo> list = academicInfoService.selectAcademicInfoList_handteacher(teacherId);
            return getDataTable(list);
        }
        else
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            startPage();
            List<AcademicInfo> list = academicInfoService.selectAcademicInfoList_Xiteacher(teacherId);
            return getDataTable(list);
        }
    }

    /**
     * 导出学业情况列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:acadInfo:export')")
    @Log(title = "学业情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcademicInfo academicInfo)
    {
        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            List<AcademicInfo> list = academicInfoService.selectAcademicInfoList(academicInfo);
            ExcelUtil<AcademicInfo> util = new ExcelUtil<AcademicInfo>(AcademicInfo.class);
            util.exportExcel(response, list, "学业情况数据");
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            List<AcademicInfo> list = academicInfoService.selectAcademicInfoList_handteacher(teacherId);
            ExcelUtil<AcademicInfo> util = new ExcelUtil<AcademicInfo>(AcademicInfo.class);
            util.exportExcel(response, list, "学业情况数据");
        }
        else
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            List<AcademicInfo> list = academicInfoService.selectAcademicInfoList_Xiteacher(teacherId);
            ExcelUtil<AcademicInfo> util = new ExcelUtil<AcademicInfo>(AcademicInfo.class);
            util.exportExcel(response, list, "学业情况数据");
        }
    }

    /**
     * 获取学业情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:acadInfo:query')")
    @GetMapping(value = "/{serialNumber}")
    public AjaxResult getInfo(@PathVariable("serialNumber") Long serialNumber)
    {
        return success(academicInfoService.selectAcademicInfoBySerialNumber(serialNumber));
    }

    /**
     * 新增学业情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:acadInfo:add')")
    @Log(title = "学业情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcademicInfo academicInfo)
    {
        return toAjax(academicInfoService.insertAcademicInfo(academicInfo));
    }

    /**
     * 修改学业情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:acadInfo:edit')")
    @Log(title = "学业情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcademicInfo academicInfo)
    {
        return toAjax(academicInfoService.updateAcademicInfo(academicInfo));
    }

    /**
     * 删除学业情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:acadInfo:remove')")
    @Log(title = "学业情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{serialNumbers}")
    public AjaxResult remove(@PathVariable Long[] serialNumbers)
    {
        return toAjax(academicInfoService.deleteAcademicInfoBySerialNumbers(serialNumbers));
    }

    //模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<AcademicInfo> util = new ExcelUtil<AcademicInfo>(AcademicInfo.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @PreAuthorize("@ss.hasPermi('stuinfo:acadInfo:adds')")
    @Log(title = "成绩详情", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult excelImport(MultipartFile file) throws Exception {
        ExcelUtil<AcademicInfo> util = new ExcelUtil<AcademicInfo>(AcademicInfo.class);
        List<AcademicInfo> academicInfos = util.importEasyExcel(file.getInputStream());
        return toAjax(academicInfoService.insertAcademicInfos(academicInfos));
    }
}
